<?php 

class Pnr_model extends CI_Model{
	
	//private $uniairline=array();
	private $sHashCode="";
	private $iOilFee=110;
	private $iAirportFee=50;
	public $Currencyrate=1;
	public $mainCurrency="RMB";
	private $arAirport=array();
	private $weblang="english";
	private $Servercharge=array();
	private $AgentCode;
	
	function __construct()
	{
	    parent::__construct();
	    
	    $this->sHashCode=$this->session->userdata("WEB_AuthorizedKey");
	    
	    $this->Currencyrate	=$this->session->userdata("WEB_Currencyrate");
	    $this->mainCurrency	=$this->session->userdata("WEB_mainCurrency");
	    $this->weblang		=$this->session->userdata("lang");
	    $this->Servercharge  =$this->session->userdata("WEB_Servercharge");
	    $this->AgentCode	=$this->session->userdata("WEB_AgentCode");
	    
	    $this->iAirportFee=$this->iAirportFee*$this->Currencyrate;
	}
	
	//航班查询并返回对应的航班数据
	//$isPFD表示是否使用私有运价
	public function avflight($sFromport,$sDestport,$dFlightdate,$sAirline,$isback=false,$FlightType,$isPFD="FD")
	{
		$arFlight=array();
		
		//获取AV数据
		$this->load->model("Air_model");
		$lstFlight=$this->Air_model->ws_avflight($sFromport,$sDestport,$dFlightdate,$sAirline);
		$iFlight=count($lstFlight);

		$iOilfee=0;
		//输出航班详细信息
		for($i=0;$i<$iFlight;$i++)
		{
			$item=explode("|",$lstFlight[$i]);
			//FM9370|20110510|0655|0920|true|738|0|F8,P2,A2,YA,KA,BA,HA,LA,MA,XA,I5,|SZX|SHA
			
			if($sAirline==substr($item[0],0,2) ||$sAirline=="")
			{
				//回程航班查询的话必须符合去程信息				
				if(!$isback || ($isback && $item[8]==$sFromport && $item[9]==$sDestport))
				{
					//print_r($item);
					//计算运价
					$arFlight[$i]=array("FlightNo" => $item[0],
									"Airline" => substr($item[0],0,2),
									"flightdate" => $item[1],
									"Depart" => implode(str_split($item[2],2),":"),
									"Arrival" => implode(str_split($item[3],2),":"),
									"Plane" => $item[5],
									"Stop" => $item[6],
									"class" => $this->getClassPrice($item[7],substr($item[0],0,2),$item[0]) ,
									"fromport" => $item[8],
									"destport" => $item[9],
									"fromportName" => $item[8],
									"destportName" => $item[9],
									"OilFee" => $iOilfee,
									"AirportFee" => $this->iAirportFee,
									"mainCurrency" => $this->mainCurrency
								);
				}
			}
		}
		return $arFlight;
	}
	
	
	//计算舱位运价
	private function getClassPrice($ClassList,$airline,$flightnum)
	{
		//F8,P2,A2,YA,KA,BA,HA,LA,MA,XA,I5,
		$arclass=explode(",",$ClassList);
		$arPrice=array();
		
		//item形如YA
		foreach($arclass as $item)
		{
			$iPrice=0;
			$sclass=substr($item,0,1);
			$iseat=substr($item,1,1);
			$pricetype="FD";
			$policyid=0;
			if($iseat=="A" || is_numeric($iseat))
			{
				//先从FD数据中找
				
				if($iseat=="A"){$iseat=">9";}
				//舱位，数量，票面价
				array_push($arPrice,array($sclass,$iseat,0,0,0));
			}	
		}
		
		//print_r($arPrice);
		return $arPrice;
	}
	
	public function create_PNR_Only($data,$user_data)
	{	
		
		//产生订单号
		ini_set("date.timezone","Asia/Shanghai");
		

		$arflight=explode('|',$data['go_flightinfo']);
		$arbackflight=explode('|',$data['back_flightinfo']);
		$ItineraryName=$arflight[5].'-'.$arflight[6].'-'.$arflight[1];
		
		$blDouble=FALSE;
		$sReturndate=null;
		//设置回程信息
		if($data['back_flightinfo']!="")
		{
			$sReturndate=$arbackflight[1];
			$blDouble=TRUE;
			$ItineraryName=$arflight[5].'-'.$arflight[6].'-'.$arbackflight[6].'-'.$arflight[1];
		}
		//先产生PNR，再产生订单
		//http://210.73.195.74:7899/webabe/webserviceabe.asmx/CreatePNR?
		//Tel=075533398989,075533398898
		//Flightinfo=CTU^SHE^ZH9452^Z^2011-05-09^17:55
		//回程信息格式，SZX,SHA^SHA,SZX^ZH9815,ZH9814^Y,V^2010-11-13,2010-11-17^17:10
		//NameList=熊钰
		//CTList=320624197212171216
		//PTel=15994381825
		////0000,HF9WMM,HKG261
		
		
		$Tel			="075533398989,075533398898";
		$FlightInfo= $arflight[5]."^".$arflight[6]."^".$arflight[2]."^".$arflight[11]."^".date('Y-m-d',strtotime($arflight[1]))."^".$arflight[3];		
		$iTotalPrice=0;
		
		if($blDouble)
		{
			$FlightInfo=$arflight[5].",".$arbackflight[5]."^".
					$arflight[6].",".$arbackflight[6]."^".
					$arflight[2].",".$arbackflight[2]."^".
					$arflight[11].",".$arbackflight[11]."^".
					date('Y-m-d',strtotime($arflight[1])).",".date('Y-m-d',strtotime($arbackflight[1]))."^".
					$arflight[3];
					
			$iTotalPrice+=$arbackflight[13]+$arbackflight[9]+$arbackflight[10];
		}
		
		$iTotalPrice*=$data['TotalAdult'];
		
		$iPassenger=count($data['PassengerName']);
		$NameList="";
		$CTList="";
		$PTel="";
		
		for($i=0;$i< $iPassenger;$i++)
		{
			//组织字符串
			$NameList.=$data['PassengerName'][$i].",";
			$CTList.=$data['CTID'][$i].",";
			$PTel.=$data['PassengerTel'][$i].",";
		}
		
		if($NameList!="")
		{
			$NameList	=substr($NameList,0,strlen($NameList)-1);
			$CTList	=substr($CTList,0,strlen($CTList)-1);
			$PTel	=substr($PTel,0,strlen($PTel)-1);
		}
		
		$this->load->Model("Air_model");
		$PNRData=$this->Air_model->ws_CreatePNR($Tel,$FlightInfo,$NameList,$CTList,$PTel);
		
		
		$fromportName=$this->Air_model->getAirportname_buffer($arflight[5]);
		$destportName=$this->Air_model->getAirportname_buffer($arflight[6]);
		
		
		if($PNRData[0]=="0000")		
		{
			$PNR=$PNRData[1];
		}else
		{
			return array(false,"未产生PNR");
		}
		
		//写日志
		$this->load->Model("Order_model");
		$this->Order_model->write_orderlog($PNR,"Create PNR","",$user_data);
		
		return array(true,$PNR);
		/*
		$query = $this->db->query('SELECT OrderNo, Name, CTID,PPID  FROM bok_cps ');

		foreach ($query->result() as $row)
		{
		    print_r($row);
		    echo "<BR/>";
		}
		
		echo 'Total Results: ' . $query->num_rows(); 
		*/

	}
	
}


?>